package cfgs.gestudent.finestra.curs;

import java.awt.Color;

import javax.swing.JPanel;

import java.awt.GridBagLayout;

import javax.swing.JButton;

import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.util.ResourceBundle;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import cfgs.gestudent.dao.curs.InserirCurs;
import cfgs.gestudent.preferencies.ConfiguracionsIdiomes;

import javax.swing.SwingConstants;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class FinestraInserirCurs extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5228616848242644825L;
	private JTextField txtCurs;
	
	private ConfiguracionsIdiomes idioma = new ConfiguracionsIdiomes();
	private ResourceBundle rb = idioma.getIdioma();
	private JTextField txtAbreviacio;

	/**
	 * Create the panel.
	 */
	public FinestraInserirCurs() {
		
		setBackground(Color.WHITE);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] {30, 0, 30, 30, 30, 0, 30};
		gridBagLayout.rowHeights = new int[] {0, 30, 0, 0, 30, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		JLabel lblNom = new JLabel(rb.getString("txtNomCurs"));
		GridBagConstraints gbc_lblNom = new GridBagConstraints();
		gbc_lblNom.anchor = GridBagConstraints.EAST;
		gbc_lblNom.insets = new Insets(0, 0, 5, 5);
		gbc_lblNom.gridx = 0;
		gbc_lblNom.gridy = 1;
		add(lblNom, gbc_lblNom);
		
		txtCurs = new JTextField();
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.gridwidth = 3;
		gbc_textField.insets = new Insets(0, 0, 5, 5);
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 1;
		gbc_textField.gridy = 1;
		add(txtCurs, gbc_textField);
		txtCurs.setColumns(10);
		
		JLabel lblAbreviacio = new JLabel(rb.getString("txtAbreviacioCurs"));
		GridBagConstraints gbc_lblAbreviacio = new GridBagConstraints();
		gbc_lblAbreviacio.anchor = GridBagConstraints.EAST;
		gbc_lblAbreviacio.insets = new Insets(0, 0, 5, 5);
		gbc_lblAbreviacio.gridx = 0;
		gbc_lblAbreviacio.gridy = 2;
		add(lblAbreviacio, gbc_lblAbreviacio);
		
		txtAbreviacio = new JTextField();
		GridBagConstraints gbc_textField_1 = new GridBagConstraints();
		gbc_textField_1.gridwidth = 3;
		gbc_textField_1.insets = new Insets(0, 0, 5, 5);
		gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_1.gridx = 1;
		gbc_textField_1.gridy = 2;
		add(txtAbreviacio, gbc_textField_1);
		txtAbreviacio.setColumns(10);
		
		JLabel lblAvisoInsercionAnyo = new JLabel(rb.getString("lblAvisoInsercionAnyo"));
		lblAvisoInsercionAnyo.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_lblAvisoInsercionAnyo = new GridBagConstraints();
		gbc_lblAvisoInsercionAnyo.gridwidth = 4;
		gbc_lblAvisoInsercionAnyo.insets = new Insets(0, 0, 5, 5);
		gbc_lblAvisoInsercionAnyo.gridx = 0;
		gbc_lblAvisoInsercionAnyo.gridy = 3;
		add(lblAvisoInsercionAnyo, gbc_lblAvisoInsercionAnyo);
		
		JButton btnCancelar = new JButton(rb.getString("btnCancelar"));
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				txtCurs.setText(null);
				txtAbreviacio.setText(null);
			}
		});
		GridBagConstraints gbc_btnCancelar = new GridBagConstraints();
		gbc_btnCancelar.insets = new Insets(0, 0, 5, 5);
		gbc_btnCancelar.gridx = 2;
		gbc_btnCancelar.gridy = 4;
		add(btnCancelar, gbc_btnCancelar);
		
		JButton btnGuardar = new JButton(rb.getString("btnGuardar"));
		btnGuardar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				InserirCurs ic = new InserirCurs();
				boolean seguir = ic.inserirCurs(txtCurs.getText(), txtAbreviacio.getText());
				
				if(seguir == true){
					//Missatge les dades han estat insertades
					JOptionPane.showMessageDialog(null, rb.getString("missatgeDadesInsertades"), rb.getString("titolDadesInsertades"), JOptionPane.INFORMATION_MESSAGE);
					txtCurs.setText(null);
					txtAbreviacio.setText(null);
				}else{
					//Missatge les dades han estat insertades
					JOptionPane.showMessageDialog(null, rb.getString("missatgeDadesNoInsertades"), rb.getString("titolDadesNoInsertades"), JOptionPane.WARNING_MESSAGE);
					
				}
			}
		});
		GridBagConstraints gbc_btnGuardar = new GridBagConstraints();
		gbc_btnGuardar.insets = new Insets(0, 0, 5, 5);
		gbc_btnGuardar.gridx = 3;
		gbc_btnGuardar.gridy = 4;
		add(btnGuardar, gbc_btnGuardar);
		

	}

}
